package com.me.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.me.MyAppleDrop.LoadingBar;
import com.me.MyAppleDrop.MainAppleDrop;
import com.me.enums.ScreenEnum;

public class LoadingScreens extends BaseScreen {

	private Image logo;
	private Image loadingFrame;
	private Image loadingBarHidden;
	private Image screenBg;
	private Image loadingBg;
	private float startX, endX;
	private float percent;
	private Actor loadingBar;
	AssetManager manager = new AssetManager();
	@Override
	public void show() {
		super.show();
//		Image backGround = new Image(MainAppleActive.getAtlas().findRegion("loading/libgdx-logo"));
//		stage.addActor(backGround);
		
		Texture.setEnforcePotImages(false);
		
		// Tell the manager to load assets for the loading Screen
		//wait until they are finished loading
		manager.finishLoading();
		//Initialize the stage where we will place everything
		stage = new Stage();

		// Grab the regions from the atlas and create some images
		logo = new Image(MainAppleDrop.getAtlas().findRegion("loading/libgdx-logo"));
		loadingFrame = new Image(MainAppleDrop.getAtlas().findRegion("loading/loading-frame"));
        loadingBarHidden = new Image(MainAppleDrop.getAtlas().findRegion("loading/loading-bar-hidden"));
        screenBg = new Image(MainAppleDrop.getAtlas().findRegion("loading/screen-bg"));
        loadingBg = new Image(MainAppleDrop.getAtlas().findRegion("loading/loading-frame-bg"));
        
        // Add the loading bar animation
        Animation anim = new Animation(0.05f, MainAppleDrop.getAtlas().findRegions("loading/loading-bar-anim"));
        anim.setPlayMode(Animation.LOOP_REVERSED);
        loadingBar = new LoadingBar(anim);
        
        // Or if you only need a static bar, you can do
        // loadingBar = new Image(atlas.findRegion("loading-bar1"));

        // Add all the actors to the stage
        stage.addActor(screenBg);
        stage.addActor(loadingBar);
        stage.addActor(loadingBg);
        stage.addActor(loadingBarHidden);
        stage.addActor(loadingFrame);
        stage.addActor(logo);
	}

	@Override
	public void render(float arg0) {
		super.render(arg0);
//		 // Clear the screen
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
//      if (manager.update()) { // Load some, will return true if done loading
//          if (Gdx.input.isTouched()) { // If the screen is touched after the game is done loading, go to the main menu screen
//        	  MainAppleActive.getScreenManager().createScreen(ScreenEnum.MENU_SCREEN);
//        	  MainAppleActive.getScreenManager().getScreen().show();
//          }
//      }
      
      float fprogress = manager.getProgress();
      if(fprogress >= 1 ){
    	  MainAppleDrop.getScreenManager().createScreen(ScreenEnum.MENU_SCREEN);
    	  MainAppleDrop.getScreenManager().getScreen().show();
      }
      
      // Interpolate the percentage to make it more smooth
      percent = Interpolation.linear.apply(percent, manager.getProgress(), 0.1f);

      // Update positions (and size) to match the percentage
      loadingBarHidden.setX(startX + endX * percent);
      loadingBg.setX(loadingBarHidden.getX() + 30);
      loadingBg.setWidth(450 - 450 * percent);
      loadingBg.invalidate();

      // Show the loading screen
      stage.act();
      stage.draw();
	}

	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
		// Set our screen to always be XXX x 480 in size
	      width = 480 * width / height;
	      height = 480;
	      stage.setViewport(width , height, false);
	
	      // Make the background fill the screen
	      screenBg.setSize(width, height);
	
	      // Place the logo in the middle of the screen and 100 px up
	      logo.setX((width - logo.getWidth()) / 2);
	      logo.setY((height - logo.getHeight()) / 2 + 100);
	
	      // Place the loading frame in the middle of the screen
	      loadingFrame.setX((stage.getWidth() - loadingFrame.getWidth()) / 2);
	      loadingFrame.setY((stage.getHeight() - loadingFrame.getHeight()) / 2);
	
	      // Place the loading bar at the same spot as the frame, adjusted a few px
	      loadingBar.setX(loadingFrame.getX() + 15);
	      loadingBar.setY(loadingFrame.getY() + 5);
	
	      // Place the image that will hide the bar on top of the bar, adjusted a few px
	      loadingBarHidden.setX(loadingBar.getX() + 35);
	      loadingBarHidden.setY(loadingBar.getY() - 3);
	      // The start position and how far to move the hidden loading bar
	      startX = loadingBarHidden.getX();
	      endX = 440;
	
	      // The rest of the hidden bar
	      loadingBg.setSize(450, 50);
	      loadingBg.setX(loadingBarHidden.getX() + 30);
	      loadingBg.setY(loadingBarHidden.getY() + 3);
		
	}
	
	

}
